<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const loading = ref(false)
const classes = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const form = reactive({ id: null, name: '' })

const fetchClasses = async () => {
  loading.value = true
  const res = await axios.get('/api/admin/class-name/list')
  if (res.data && res.data.code === '200') {
    classes.value = res.data.data
  }
  loading.value = false
}

const handleAdd = () => {
  isEdit.value = false
  form.id = null
  form.name = ''
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEdit.value = true
  form.id = row.id
  form.name = row.name
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该班级吗？', '提示', { type: 'warning' })
    .then(async () => {
      await axios.delete(`/api/admin/class-name/${row.id}`)
      ElMessage.success('删除成功')
      fetchClasses()
    })
}

const handleSubmit = async () => {
  if (!form.name) {
    ElMessage.warning('请输入班级名称')
    return
  }
  if (isEdit.value) {
    await axios.put('/api/admin/class-name/update', { id: form.id, name: form.name })
    ElMessage.success('修改成功')
  } else {
    await axios.post('/api/admin/class-name/add', { name: form.name })
    ElMessage.success('添加成功')
  }
  dialogVisible.value = false
  fetchClasses()
}

onMounted(fetchClasses)
</script>

<template>
  <div class="manage-bg">
    <el-card>
      <template #header>
        <div class="manage-header">
          <span>班级管理</span>
          <el-button type="primary" @click="handleAdd">新增班级</el-button>
        </div>
      </template>
      <el-table :data="classes" v-loading="loading" style="width: 100%;">
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="name" label="班级名称" align="center" />
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑班级' : '新增班级'" width="600px" class="center-dialog">
      <el-form :model="form" label-width="80px" class="dialog-form">
        <el-form-item label="名称">
          <el-input v-model="form.name" placeholder="请输入班级名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.manage-bg {
  background: #fff;
  min-height: 100vh;
  padding: 30px 0;
}
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.center-dialog :deep(.el-dialog__body) {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 0 0;
}
.dialog-form {
  width: 100%;
  display: flex;
  justify-content: center;
}
.dialog-form .el-form-item {
  width: 100%;
}
</style> 